package com.higold.cloud.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v4.app.FragmentTransaction
import android.view.KeyEvent
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.gyf.immersionbar.ktx.immersionBar
import com.higold.cloud.R
import com.higold.cloud.base.BaseActivity
import com.higold.cloud.mvp.model.bean.TabEntity
import com.higold.cloud.showToast
import com.higold.cloud.ui.fragment.*
import com.higold.cloud.utils.StatusBarUtil
import kotlinx.android.synthetic.main.activity_main.*
import java.util.ArrayList

/**
 *    author : lxz
 *    date   : 2019/6/25 10:22
 *    desc   :
 */
class NewMainActivity : BaseActivity() {


    private val mTitles = arrayOf("报表", "业务员", "代理商", "个人中心")

    // 未被选中的图标
    private val mIconUnSelectIds = intArrayOf(R.drawable.ic_report_n, R.drawable.ic_manager_n, R.drawable.ic_agent_n, R.drawable.ic_me_n)
    // 被选中的图标
    private val mIconSelectIds = intArrayOf(R.drawable.ic_report_o, R.drawable.ic_manager_o, R.drawable.ic_agent_o, R.drawable.ic_me_o)

    private val mTabEntities = ArrayList<CustomTabEntity>()

    private var mHomeFragment: NewHomeFragment? = null
    private var mSalesmanFragment: SalesmanFragment? = null
    private var mAgentFragment: NewAgentFragment? = null
    private var mUserFragment: UserFragment? = null

    //默认为0
    private var mIndex = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        if (savedInstanceState != null) {
            mIndex = savedInstanceState.getInt("currTabIndex")
        }
        super.onCreate(savedInstanceState)
        initTab()
        tab_layout.currentTab = mIndex
        switchFragment(mIndex)

    }

    override fun layoutId(): Int {
        return R.layout.activity_main
    }


    //初始化底部菜单
    private fun initTab() {
        (0 until mTitles.size)
                .mapTo(mTabEntities) { TabEntity(mTitles[it], mIconSelectIds[it], mIconUnSelectIds[it]) }
        //为Tab赋值
        tab_layout.setTabData(mTabEntities)
        tab_layout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                //切换Fragment
                switchFragment(position)
            }

            override fun onTabReselect(position: Int) {

            }
        })
    }

    /**
     * 切换Fragment
     * @param position 下标
     */
    private fun switchFragment(position: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        when (position) {
            0 // 报表
            -> mHomeFragment?.let {
                transaction.show(it)
                immersionBar {
                    statusBarColor(R.color.colorAccent)
                    titleBarMarginTop(fl_container)
                    statusBarDarkFont(false)
                    navigationBarDarkIcon(false)
                }
            } ?: NewHomeFragment.getInstance(mTitles[position]).let {
                mHomeFragment = it
                transaction.add(R.id.fl_container, it, "home")
            }
            1  //业务员
            -> mSalesmanFragment?.let {
                transaction.show(it)
                immersionBar {
                    statusBarColor(R.color.colorAccent)
                    titleBarMarginTop(fl_container)
                    statusBarDarkFont(false)
                    navigationBarDarkIcon(false)
                }
            } ?: SalesmanFragment.getInstance(mTitles[position]).let {
                mSalesmanFragment = it
                transaction.add(R.id.fl_container, it, "discovery") }
            2  //代理商
            -> mAgentFragment?.let {
                transaction.show(it)
                immersionBar {
                    statusBarColor(R.color.colorAccent)
                    titleBarMarginTop(fl_container)
                    statusBarDarkFont(false)
                    navigationBarDarkIcon(false)
                }
            } ?: NewAgentFragment.getInstance(mTitles[position]).let {
                mAgentFragment = it
                transaction.add(R.id.fl_container, it, "hot") }
            3 //个人中心
            -> mUserFragment?.let {
                transaction.show(it)
                immersionBar {
                    statusBarColor(R.color.colorPrimary)
                    titleBarMarginTop(fl_container)
                    statusBarDarkFont(true)
                    navigationBarDarkIcon(true)
                }
            } ?: UserFragment.getInstance(mTitles[position]).let {
                mUserFragment = it
                transaction.add(R.id.fl_container, it, "mine") }

            else -> {

            }
        }

        mIndex = position
        tab_layout.currentTab = mIndex
        transaction.commitAllowingStateLoss()
    }


    /**
     * 隐藏所有的Fragment
     * @param transaction transaction
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        mHomeFragment?.let { transaction.hide(it) }
        mSalesmanFragment?.let { transaction.hide(it) }
        mAgentFragment?.let { transaction.hide(it) }
        mUserFragment?.let { transaction.hide(it) }
    }


    @SuppressLint("MissingSuperCall")
    override fun onSaveInstanceState(outState: Bundle) {
//        showToast("onSaveInstanceState->"+mIndex)
//        super.onSaveInstanceState(outState)
        //记录fragment的位置,防止崩溃 activity被系统回收时，fragment错乱
        if (tab_layout != null) {
            outState.putInt("currTabIndex", mIndex)
        }
    }

    override fun initView() {
        immersionBar {
            statusBarColor(R.color.colorAccent)
            titleBarMarginTop(fl_container)
        }
    }

    override fun initData() {

    }

    override fun start() {

    }

    private var mExitTime: Long = 0

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis().minus(mExitTime) <= 2000) {
                finish()
            } else {
                mExitTime = System.currentTimeMillis()
                showToast("再按一次退出程序")
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }


}
